//
//  ZLBaseNavigationController.m
//
//  Created by Jiaozl on 2022/6/2.
//
//

#import "ZLBaseNavigationController.h"
#import "UIColor+Additions.h"

@interface ZLBaseNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation ZLBaseNavigationController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc {
    NSLog(@"Jiaozl %s", __func__);
}


#pragma mark - Private Method

- (void)setupUI {
//    self.navigationBar.tintColor = UIColor.greenColor;
    
//    self.navigationBar.translucent = NO;
//    self.tabBarController.tabBar.translucent = NO;
    
    // 一种方式是使用iOS 13之后的写法
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        appearance.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithHex:0x333333]};
        appearance.backgroundColor = UIColor.whiteColor;
        UINavigationBar.appearance.standardAppearance = appearance;
        UINavigationBar.appearance.scrollEdgeAppearance = appearance;
        UINavigationBar.appearance.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
    } else {  // 另一种方式是之前的写法 (在 iOS 15 上页面切换 导航栏有一个灰色效果，其他系统版本未尝试)
        self.navigationBar.barTintColor = UIColor.redColor;
        self.navigationBar.barStyle = UIBarStyleDefault;
        self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithHex:0x333333]};
//        [self.navigationBar setBackgroundColor:UIColor.clearColor];
        [self customeBarContent];
        self.view.backgroundColor = UIColor.redColor; // 关键在于这句
//    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];
    }
    self.interactivePopGestureRecognizer.delegate = self;
}

/*自定义导航栏内容*/
- (void)customeBarContent {
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    NSDictionary *textAttributes = @{
                                     NSFontAttributeName : [UIFont boldSystemFontOfSize:17],
                                     NSForegroundColorAttributeName : [UIColor colorWithHex:0x333333],
                                     };
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
//                                                         forBarMetrics:UIBarMetricsDefault];
    [navigationBarAppearance setTitleTextAttributes:textAttributes];

}


#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return self.childViewControllers.count > 1;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return self.viewControllers.count > 1;
}

#pragma mark - Getter

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.topViewController.preferredStatusBarStyle;
}

@end
